$(document).ready(function () {
	if (getCookie("phoneNum") !== "" && getCookie("password") !== "") {
		$("#phoneNum").val(getCookie("phoneNum"));
		$("#password").val(getCookie("password"))
	}
});

function shiyongClick() {
	xtip.win({
		type: "confirm",
		btn: ["知道了"],
		tip: '放心使用，不会记录你的任何信息，白给我都不要，原理就是伪装浏览器请求了乐心的步数接口：<br><br>1. 基于乐心健康app，请载乐心健康app：官方下载地址：<a href="http://www.lifesense.com/app/" target="_blank">http://www.lifesense.com/app/</a>，手机号注册账号</br>2. 登录之后，点击我的->设置->账号与安全->设置密码(修改密码)，设置你自己记得住的密码</br> 3. 回到App首页，点击我的->数据共享，绑定你想同步数据的项目, 注：同步微信运动请按照要求关注【乐心运动】公众号</br>4. 回到网站，填写信息后开始刷步即可同步至你绑定的所有平台</br></br>',
		icon: "success",
		title: "食用方法",
		min: true,
		width: "600px",
		shade: false,
		shadeClose: false,
		lock: false,
		zindex: 99999,
	})
}

function getCookie(b) {
	var a, c = new RegExp("(^| )" + b + "=([^;]*)(;|$)");
	if (a = document.cookie.match(c)) {
		return unescape(a[2])
	} else {
		return null
	}
}

function shuabuClick() {
	if ($("#phoneNum").val() == "") {
		xtip.alert("请输入账号", "e")
	} else {
		if ($("#password").val() == "") {
			xtip.alert("请输入密码", "e")
		} else {
			if ($("#stepNum").val() == "") {
				xtip.alert("请输入步数", "e")
			} else {
				if (getCookie("phoneNum") == "" || getCookie("password") == "" || getCookie("userId") == "" || getCookie("accessToken") == "") {
					login()
				} else {
					if (getCookie("phoneNum") !== $("#phoneNum").val() || getCookie("password") !== $("#password").val()) {
						login()
					} else {
						submitstep($("#stepNum").val(), getCookie("userId"), getCookie("accessToken"))
					}
				}
			}
		}
	}
}

function submitstep(a, b, c) {
	var d = {
		list: [{
			calories: Math.floor(a / 40),
			dataSource: "3",
			deviceId: "M_NULL",
			distance: Math.floor(a * 3 / 5),
			userId: b,
			measurementTime: getdate(),
			type: "0",
			priority: "0",
			created: Date.parse(new Date()),
			step: a
		}],
		timestamp: Date.parse(new Date())
	};
	$.ajax({
		url: "/stepSub",
		dataType: "json",
		async: true,
		data: JSON.stringify(d),
		type: "post",
		xhrFields: {
			withCredentials: true
		},
		headers: {
			"Content-Type": "application/json; charset=utf-8",
		},
		beforeSend: function (e) {},
		success: function (e) {
			xtip.closeAll();
			if (e.code === 200) {
				xtip.alert("刷步成功，请勿连续刷步", "s")
			} else {
				if (e.code === 401) {
					login()
				} else {
					xtip.alert(e.msg, "e")
				}
			}
		},
		error: function (e) {
			xtip.closeAll();
			xtip.alert("出现了点小问题，请右上角联系站长", "e")
		}
	})
}

function login() {
	$.ajax({
		url: "/stepLogin",
		dataType: "json",
		async: true,
		data: JSON.stringify({
			"appType": "6",
			"clientId": md5(9527),
			"loginName": $("#phoneNum").val(),
			"password": md5($("#password").val()),
			"timestamp": Date.parse(new Date())
		}),
		type: "post",
		headers: {
			"Content-Type": "application/json; charset=utf-8",
		},
		beforeSend: function (a) {
			xtip.load("刷步中...")
		},
		success: function (a) {
			xtip.closeAll();
			if (a.code === 200) {
				setCookie("accessToken", a.data.accessToken, "d30");
				setCookie("phoneNum", $("#phoneNum").val(), "d30");
				setCookie("userId", a.data.userId, "d30");
				setCookie("password", $("#password").val(), "d30");
				submitstep($("#stepNum").val(), a.data.userId, a.data.accessToken)
			} else {
				xtip.alert(a.msg, "e")
			}
		},
		error: function (a) {
			xtip.closeAll();
			xtip.alert("出现了点小问题，请右上角联系站长", "e")
		}
	})
}

function getdate() {
	var c = new Date();
	var e = c.getFullYear();
	var f = c.getMonth() + 1 < 10 ? "0" + (c.getMonth() + 1) : c.getMonth() + 1;
	var b = c.getDate() < 10 ? "0" + c.getDate() : c.getDate();
	var a = c.getHours() < 10 ? "0" + c.getHours() : c.getHours();
	var d = c.getMinutes() < 10 ? "0" + c.getMinutes() : c.getMinutes();
	var g = c.getSeconds() < 10 ? "0" + c.getSeconds() : c.getSeconds();
	return e + "-" + f + "-" + b + " " + a + ":" + d + ":" + g
}

function setCookie(a, c, d) {
	var b = getsec(d);
	var e = new Date();
	e.setTime(e.getTime() + b * 1);
	document.cookie = a + "=" + escape(c) + ";expires=" + e.toGMTString()
}

function getsec(c) {
	var b = c.substring(1, c.length) * 1;
	var a = c.substring(0, 1);
	if (a == "s") {
		return b * 1000
	} else {
		if (a == "h") {
			return b * 60 * 60 * 1000
		} else {
			if (a == "d") {
				return b * 24 * 60 * 60 * 1000
			}
		}
	}
};
